import Vue from 'vue';
import VueRouter from "vue-router";
import Hlogin from "../views/Hlogin";
import Hindex from "../views/Hindex";
import Shuju from "../views/Shuju";
import Permis from "../views/Permis/Permis";
import Tong from "../views/Tong";
import AddSong from "../views/Songs/SongsChilder/AddSong";
import FindSong from "../views/Songs/SongsChilder/FindSong";
import UpdateSong from "../views/Songs/SongsChilder/UpdateSong";
import Singers from "../views/Singers/Singers";
import Users from "../views/Permis/Childer/Users";
import Add from "../views/Permis/Childer/AdminChilder/Add";
import Update from "../views/Permis/Childer/AdminChilder/Update";

Vue.use(VueRouter);

const router = new VueRouter({
    routes: [
        {path:'/', redirect: '/Shuju'},
        {path: '/Hindex', component: Hindex,
        children:[
            {path: '/Shuju', name:'Shuju', component: Shuju},
            {path: '/Permis', name:'Permis', component: Permis},
            {path: '/Users', name:'Users', component: Users},
            {path: '/Add', name:'Add', component: Add},
            {path: '/Update', name:'Update', component: Update},
            {path: '/Singers', name:'Singers', component: Singers},
            {path: '/AddSong', name:'AddSong', component: AddSong},
            {path: '/FindSong', name:'FindSong', component: FindSong},
            {path: '/UpdateSong', name:'UpdateSong', component: UpdateSong},
            {path: '/Tong', name:'Tong', component: Tong},
        ]},
        {path: '/Login', component: Hlogin}
    ]
});
// to 表示将要访问的路由的信息对象
// from 表示将要离开的路由的信息对象
// next() 函数表示放行的意思
    router.beforeEach(function(to, from, next) {
    if (to.path === '/Shuju') {
        // 要访问后台主页，需要判断是否有 token
        const token = localStorage.getItem('token')
        if (token) {
            next()
        } else {
            // 没有登录，强制跳转到登录页
            next('/Login')
        }
    } else {
        next()
    }
})
export default router